package com.tianyang.yukuaifuzs.business.trade.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.WithdrawOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.WithdrawContract;
import com.tianyang.yukuaifuzs.business.trade.model.TradeModel;
import com.tianyang.yukuaifuzs.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.module.dialog.HintDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifuzs.utils.IntentTool;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：提现
 * Created by xyj on 2024/3/11
 */
public class WithdrawPresenter implements WithdrawContract.Presenter {

    private WithdrawContract.View mView;

    private CompositeSubscription mSubscription;

    private int agentId;

    public WithdrawPresenter(WithdrawContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            agentId = output.getAgentId();
        }
    }

    @Override
    public void postixian(String money) {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().postixian(agentId, money)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject output) {
                        new HintDialog(mView.getContext())
                                .title("温馨提示")
                                .content("提现申请成功")
                                .listener(new OnBtnClickL() {
                                    @Override
                                    public void onBtnClick() {
                                        TradeOpenProvider.getInstance().openWithdrawRecordActivity(mView.getContext());
                                        IntentTool.finishActivity(mView.getContext());
                                    }
                                }).show();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getcantixian() {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().getcantixian(agentId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WithdrawOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(WithdrawOutput output) {
                        mView.showWithdrawView(output);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void queryUserSettlementCard() {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().queryUserSettlementCard(true)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettleBankOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(SettleBankOutput output) {
                        mView.showSettlementCardView(output);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
